# -*- coding: utf-8 -*-
# @Time: 2025/9/26 21:28
# @Author: foxhuty
# @File: pydantic_notes.py
from pydantic import BaseModel, ValidationError
from datetime import date


class Student(BaseModel):
    id: int
    name: str
    date_joined: date | None
    age: int | None
    birth_date: date
    hobbies: list[str]=[]
    scores: dict[str, int]={}


external_data = {
    # "id": "1001aa",
    "id": 1001,
    "name": "Tommy",
    "date_joined": "2023-01-01",
    "age": 18,
    "birth_date": "2005-05-15",
    "hobbies": ["篮球", "足球", "跑步"],
    "scores": {"语文": 90, "数学": 85, "英语": 95, "物理": 88}
}
if __name__ == '__main__':
    try:
        student = Student(**external_data)
        print(student.hobbies[1])
        print(student.scores['语文'])

    except ValidationError as e:
        print(e.errors())


